// pages/me/me.js
//获取应用实例
const app = getApp()
Page({

  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    //顶部导航栏
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    //openId
    openid:null,
    userPhone:"未绑定",
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
      //获得openId
      this.getOpenid();
    } else {
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
           
          }
      })
       //获得openId
       this.getOpenid();
    }
    this.getOpenid();
  },
  InitData(){
    if(app.globalData.openId==null){
      return;
    }
    if(this.data.hasUserInfo==false){
      return;
    }
    var that=this
    wx.request({
      url: 'https://www.yujiancs.cn/UserInfo/SelectUser',
      data:{
        wxid:app.globalData.openId
      },  method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.data[0].Tel==null){
          that.setData({
            userPhone:"未绑定"
         })
         return;
        }
          that.setData({
             userPhone:res.data[0].Tel
          })
      }
    })
  },
  // 获取用户openid
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ' + res.result.openid)
        that.setData({
          openid: res.result.openid
        })

        //保存到全局变量
        app.globalData.openId=res.result.openid;
        
        //获取到用户openid之后，保存到storage
        wx.setStorage({
                key: 'OpenId',
                data:res.result.openid,
         })
          //初始化数据
          this.InitData();
      }
    })
  },
  getUserInfo: function (e) {
    var that=this;
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    //判断用户是否接受请求登录
    if(e.detail.errMsg=="getUserInfo:ok"){      
      //注册到服务器
      that.Registered(e);
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }else{
      console.log("用户拒绝了登录请求")
    } 
  },
  /**
   * 注册到服务器
   */
  Registered(e){
    var that=this;
    console.log(e)
    wx.request({
      url: 'https://www.yujiancs.cn/UserInfo/AddInfo', //仅为示例，并非真实的接口地址
      data: {
       WxID:that.data.openid,
       Name:e.detail.userInfo.nickName,
       Pic:e.detail.userInfo.avatarUrl
      },
      method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        if(res.data.state==true){
          that.InitData();
        }
      }
    })
  },
  /**
   * 跳转获取路径
   */
  goGetLocation(){
    wx.navigateTo({
      url: '/pages/getLocation/getLocation',
    })
  },
  goContactUs(){
    wx.navigateTo({
      url: '/pages/contactUs/contactUs',
    })
  },
  login() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },goUserInfoSet(){
    wx.navigateTo({
      url: '/pages/userInfoSet/userInfoSet',
    })
  },
  goAboutUs(){
    wx.navigateTo({
      url: '/pages/aboutUs/aboutUs',
    })
  },goMyOrders(){
    wx.navigateTo({
      url: '/pages/myOrders/myOrders',
    })
  },
  goBecomeErrands(){
    wx.navigateTo({
      url: '/pages/becomeErrands/becomeErrands',
    })
  },
  onShow(){
    this.onLoad();
  }

})
